//
//  InvestListViewController.m
//  TenThousand
//
//  Created by 我的 on 16/7/19.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "InvestListViewController.h"
#import "InvestOtherCell.h"
#import "BorrowListModel.h"
#import "CurrentViewController.h"
#import "InvestFirstCell.h"
#import "ZhuanRangViewController.h"
/**
 *  定期推荐列表页面
 */

@interface InvestListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLastPage;
    NSInteger _currentPage;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *borrowArr;
@end

@implementation InvestListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
       self.navigationItem.title = @"萬车宝";
    
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    
    
    if (self.index == 0) {
        
        
        [self refreshPageWithMethod:@"Borrow.GetList"];
        [self.tableView.mj_header beginRefreshing];
    }else
    {
        [self refreshPageWithMethod:@"Transfer.GetList"];
        [self.tableView.mj_header beginRefreshing];
    }
   
}

//存储定期列表获取的数据
-(NSMutableArray *)borrowArr{
    if (!_borrowArr) {
        _borrowArr = [NSMutableArray array];
    }
    return _borrowArr;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.index == 0) {
        InvestOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InvestOtherCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"InvestOtherCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.borrowArr.count) {
            BorrowListModel *model = self.borrowArr[indexPath.row];
            
            [cell setDataWithModel:model];
        }
        //cell添加阴影
        cell.bottomView.layer.shadowColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00].CGColor;
        cell.bottomView.layer.shadowOffset =CGSizeMake(0, 1);;
        cell.bottomView.layer.shadowOpacity = YES;
        return cell;

    }else{
        
        InvestFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FIRSTCELL"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"InvestFirstCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.borrowArr.count) {
            BorrowListModel *model = self.borrowArr[indexPath.row];
            
            [cell setDataWithModel:model];
        }
        
        return cell;
        
        
    }
    return nil;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.index == 0) {
        return 150;
    }
    return 128;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

//所有定期推荐列表接口
- (void)getAllTimeDepositListWithMethend:(NSString *)methend withPage:(NSInteger)page{
    NSString *pagea = [NSString stringWithFormat:@"%ld",page];
    
    NSDictionary *dict = @{
                           @"PageIndex":pagea
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:methend withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        
        [self.borrowArr removeAllObjects];
        if (self.index == 0) {
            for (NSDictionary *dic in [object[@"Data"] firstObject][@"List"]) {
                BorrowListModel *model = [BorrowListModel mj_objectWithKeyValues:dic];
                NSLog(@"%@",model.FaSate);
                [_borrowArr addObject:model];
            }

        }else{
            for (NSDictionary *dic in [object[@"Data"] firstObject][@"ItemList"]) {
                BorrowListModel *model = [BorrowListModel mj_objectWithKeyValues:dic];
                
                [_borrowArr addObject:model];
            }

        }
            [self.tableView reloadData];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
    } progress:^(float progress) {
        
    }];
}



//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    //跳转到定期明细页面
    if (self.index == 0) {
        CurrentViewController *payMoneyView = [[CurrentViewController alloc] init];
        [self setHidesBottomBarWhenPushed:YES];
        
        if (self.borrowArr.count) {
            BorrowListModel *model  = self.borrowArr[indexPath.row];
            payMoneyView.borrowId = model.BorrowId;
            payMoneyView.item = [NSString stringWithFormat:@"%@%@",model.ProjectName,model.Code];
            payMoneyView.percentStr = model.proportion;
        }
        [self.navigationController pushViewController:payMoneyView animated:YES];

    }else{
        //跳转到债权转让页面
        ZhuanRangViewController *payMoneyView = [[ZhuanRangViewController alloc] init];
        
        if (self.borrowArr.count) {
            BorrowListModel *model = self.borrowArr[indexPath.row];
            payMoneyView.Borrowid = model.Borrowid;
            payMoneyView.TransferId = model.TransferId;
            [self setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:payMoneyView animated:YES];
            
            [self setHidesBottomBarWhenPushed:NO];

        }
        
    }
    
    
}


//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                     {
                                         
                                         [self getAllTimeDepositListWithMethend:method withPage:1];
                                         
                                         [self.tableView.mj_header endRefreshing];
                                     }];
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.tableView.mj_footer endRefreshing];
            return;
        }
        
        [self getAllTimeDepositListWithMethend:method withPage:_currentPage++];
        
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}

@end
